//
//  UIView+Extend.swift
//  PalestInk
//
//  Created by HeHeAdmin on 2019/5/20.
//  Copyright © 2019 HeHeAdmin. All rights reserved.
//

import UIKit
//swift-view任意位置添加边框线（实线，虚线）
public struct UIRectSide: OptionSet {
    public let rawValue: Int
    public static let left = UIRectSide(rawValue: 1 << 0)
    public static let top = UIRectSide(rawValue: 1 << 1)
    public static let right = UIRectSide(rawValue: 1 << 2)
    public static let bottom = UIRectSide(rawValue: 1 << 3)
    public static let all: UIRectSide = [.top, .right, .left, .bottom]
    public init(rawValue: Int) {
        self.rawValue = rawValue
    }
}

extension UIView {
    
   /// 渐变色
   ///使用方法： self.view.gradienteBackground(colors: (initColor: UIColor.hexadecimalColor(hexadecimal: "#6A82FB"), endColor: UIColor.hexadecimalColor(hexadecimal: "#FC5C7D")), orientation: .bottomRightTopLeft)
   /// - Parameters:
   ///   - colors: 颜色
   ///   - orientation: 方向
   public func gradienteBackground(colors: GradientColors, orientation: GradientOrientation) {
        let gradient = CAGradientLayer()
        
        gradient.colors = [colors.initColor.cgColor, colors.endColor.cgColor]
        gradient.startPoint = orientation.points().startPoint
        gradient.endPoint = orientation.points().endPoint
        gradient.frame = bounds
        self.layer.insertSublayer(gradient, at: 0)
    }
}
    
    extension UIView {
        
        /// 绘制横行虚线
        ///
        /// - Parameters:
        ///   - strokeColor: 横线颜色
        ///   - lineWidth: 线宽
        ///   - lineLength: 线长
        ///   - lineSpacing: 线之间间隔
        ///   - isBottom:
      public  func drawDashLine(strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 10, lineSpacing: Int = 5, isBottom: Bool = true) {
            let shapeLayer = CAShapeLayer()
            shapeLayer.bounds = self.bounds
            shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
            shapeLayer.fillColor = UIColor.blue.cgColor
            shapeLayer.strokeColor = strokeColor.cgColor
            shapeLayer.lineWidth = lineWidth
            shapeLayer.lineJoin = CAShapeLayerLineJoin.round
            //每一段虚线长度 和 每两段虚线之间的间隔
            shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
            let path = CGMutablePath()
            let y = isBottom == true ? self.layer.bounds.height - lineWidth : 0
            path.move(to: CGPoint(x: 0, y: y))
            path.addLine(to: CGPoint(x: self.layer.bounds.width, y: y))
            shapeLayer.path = path
            self.layer.addSublayer(shapeLayer)
        }
        
        /// 绘制竖直虚线
        ///
        /// - Parameters:
        ///   - strokeColor: 横线颜色
        ///   - lineWidth: 线宽
        ///   - lineLength: 线长
        ///   - lineSpacing: 线之间间隔
       public func drawVerticalLine(strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 10, lineSpacing: Int = 5) {
            let shapeLayer = CAShapeLayer()
            shapeLayer.bounds = self.bounds
            shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
            shapeLayer.fillColor = UIColor.blue.cgColor
            shapeLayer.strokeColor = strokeColor.cgColor
            shapeLayer.lineWidth = lineWidth
            shapeLayer.lineJoin = CAShapeLayerLineJoin.round
            //每一段虚线长度 和 每两段虚线之间的间隔
            shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
            let path = CGMutablePath()
            path.move(to: CGPoint(x: 0, y: lineWidth));
            path.addLine(to: CGPoint(x: 0, y: self.layer.bounds.height));
            shapeLayer.path = path
            self.layer.addSublayer(shapeLayer)
        }
        
        
        
        ///画虚线边框
       public func drawDashLine(strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 10, lineSpacing: Int = 5, corners: UIRectSide){
            let shapeLayer = CAShapeLayer()
            shapeLayer.bounds = self.bounds
            shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
            shapeLayer.fillColor = UIColor.blue.cgColor
            shapeLayer.strokeColor = strokeColor.cgColor
            shapeLayer.lineWidth = lineWidth
            shapeLayer.lineJoin = CAShapeLayerLineJoin.round
            //每一段虚线长度 和 每两段虚线之间的间隔
            shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
            let path = CGMutablePath()
            if corners.contains(.left) {
                path.move(to: CGPoint(x: 0, y: self.layer.bounds.height))
                path.addLine(to: CGPoint(x: 0, y: 0))
            }
            if corners.contains(.top){
                path.move(to: CGPoint(x: 0, y: 0))
                path.addLine(to: CGPoint(x: self.layer.bounds.width, y: 0))
            }
            if corners.contains(.right){
                path.move(to: CGPoint(x: self.layer.bounds.width, y: 0))
                path.addLine(to: CGPoint(x: self.layer.bounds.width, y: self.layer.bounds.height))
            }
            if corners.contains(.bottom){
                path.move(to: CGPoint(x: self.layer.bounds.width, y: self.layer.bounds.height))
                path.addLine(to: CGPoint(x: 0, y: self.layer.bounds.height))
            }
            shapeLayer.path = path
            self.layer.addSublayer(shapeLayer)
        }
}
